<?php
include('connect.php');
function listChungLoai()
{
	//global $connect_db;
	$query='select * from chungloai';
	$result=mysql_query($query);
	if(!$result){
		return false;
	}
	while($row=mysql_fetch_array($result,MYSQL_ASSOC))
	{
		$listChungLoai[]=$row;
	}
	return $listChungLoai;
}
function getChungLoai($idChungLoai)
{
	$query='select * from chungloai where idCL='.$idChungLoai;
	$result=mysql_query($query);
	if(!$result){
		return false;
	}
	else{
		return mysql_fetch_row($result);
	}
}
function listMenu($parent_id,$listChungLoai,$listOption=false,$idParentSelected='',$return_text='',$separator='')
{
	if($listChungLoai!=null)
	{
	foreach($listChungLoai as $chungLoai)
	{
		if($chungLoai["idParent"]==$parent_id)
		{
			if($listOption==false)
			{
			$rowMenu='<a href="show_sanpham.php?id='.$chungLoai["idCL"].'">'.$separator.$chungLoai["TenCL"].'</a></br>';
			$return_text.=listMenu($chungLoai["idCL"],$listChungLoai,$listOption,'',$rowMenu,$separator.'---');

			}
			else
			{
			$rowMenu='<option';//thực hiện nối chuỗi, xét xem trong tag select option nào sẽ hiển thị(option chủng loại cha của chủng loại hiện hành sẽ hiển thị
			if($idParentSelected==$chungLoai["idCL"])
			{
				$rowMenu.=' selected';
			}
			 $rowMenu.=' value="'.$chungLoai["idCL"].'">'.$separator.$chungLoai["TenCL"].'</option>';
			$return_text.=listMenu($chungLoai["idCL"],$listChungLoai,$listOption,$idParentSelected,$rowMenu,$separator.'---');
			}
		}
	}
	return $return_text;
	}
}
/*function listMenu($parent_id,$listChungLoai,$return_text='',$separator='')
{
	foreach($listChungLoai as $chungLoai)
	{
		if($chungLoai[2]==$parent_id)
		{
			$rowMenu=$separator.'<a href="category.php?id='.$chungLoai[0].'">'.$chungLoai[1].'</a><br />';
			$return_text.=listMenu($chungLoai[0],$listChungLoai,$rowMenu,$separator.'--');
		}
	}
	return $return_text;
}*/
//$listchungloai=listChungLoai();
//print_r($listchungloai);exit();
//$listmenu=listMenu('0',$listchungloai);
//var_dump($listmenu);exit();
function themChungLoai($idParent,$tenChungLoai,$anHien)
{
	$query='insert into chungloai(idParent,TenCL,AnHien) values('.$idParent.',"'.$tenChungLoai.'",'.$anHien.')';
	if(!mysql_query($query))
	{
		echo mysql_error();
		return false;
	}
	else
	{
		return true;
	}
}
function xoaChungLoai($idChungLoai)
{
	$query='delete from chungloai where idCL='.$idChungLoai;
	if(!mysql_query($query))
	{
		return false;
	}
	else
	{
		return true;
	}
}
function suaChungLoai($idParent,$idChungLoai,$tenChungLoai,$anHien)
{
	$query='update chungloai set idParent='.$idParent.',TenCL="'.$tenChungLoai.'",AnHien='.$anHien.' where idCL='.$idChungLoai;
	if(!mysql_query($query))
	{
		echo mysql_error();
		return false;
	}
	else
	{
		return true;
	}
}
?>